﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Text;
using System.IO;
using Common;

namespace webs.website_admin.hr
{
    public partial class jobslist : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Expires = -1;
            PageBegin();

            switch (RequestBase.GetFormString("otype"))
            {
                case "loadingdata":
                    LoadingData();
                    break;
                case "deleteitems":
                    Delete();
                    break;
            }

            if (!IsPostBack)
                CategoryDataBind();
        }

        #region 类别列表数据绑定
        void CategoryDataBind()
        {
            DataTable dt = BLL.CommonBLL.Select("KbCategoryBase", "*", " AND DeletionStateCode = 0 AND CategoryType = 1 ORDER BY LineNum ASC");
            if (dt != null && dt.Rows.Count > 0)
            {
                selCategoryList.Items.Add(new ListItem("\t+\t" + dt.Select("CategoryNumber = '" + RequestBase.GetQueryString("c_number") + "'")[0]["CategoryName"].ToString(), RequestBase.GetQueryString("c_number")));

                int layer = int.Parse(dt.Select("CategoryNumber = '" + RequestBase.GetQueryString("c_number") + "'")[0]["Layer"].ToString()) + 1;

                DropdownlistDataBind(layer, dt.Select("CategoryNumber = '" + RequestBase.GetQueryString("c_number") + "'")[0]["CategoryId"].ToString(), dt);
            }
            else
                DropdownlistDataBind(1, "00000000-0000-0000-0000-000000000000", dt);
        }
        /// <summary>
        /// 类别列表邦定
        /// </summary>
        /// <param name="intLayer">级别</param>
        /// <param name="parentId">父目录ID</param>
        /// <param name="dt">目录数据表</param>
        void DropdownlistDataBind(int intLayer, string parentId, DataTable dt)
        {
            if (dt == null)
                return;

            DataRow[] dr = dt.Select("Layer = " + intLayer.ToString() + " AND ParentId = '" + parentId + "'");

            ArrayList al = new ArrayList();
            ListItem li;

            string str = ""; int intCategoryLayerID = 0;
            foreach (DataRow d in dr)
            {
                str = "　| ";
                intCategoryLayerID = int.Parse(d["Layer"].ToString());
                if (intLayer == 1)
                    str = " + ";
                for (int i = 1; i < intLayer * intLayer; i++)
                    str += "--";


                li = new ListItem("\t" + str + "\t" + d["CategoryName"].ToString(), d["CategoryNumber"].ToString());

                selCategoryList.Items.Add(li);
                DropdownlistDataBind(intCategoryLayerID + 1, d["CategoryId"].ToString().Trim(), dt);
            }
        }
        #endregion

        #region 信息绑定
        void LoadingData()
        {
            string ctrl = "~/website_admin/hr/_controls/ctlItemsList.ascx";

            string SQL = " AND DeletionStateCode = 0";

            if (!string.IsNullOrEmpty(RequestBase.GetFormString("title")))
            {
                SQL += " AND Title LIKE '%" + RequestBase.GetFormString("title") + "%'";
            }


            SQL += " AND CategoryNumber LIKE '" + RequestBase.GetFormString("category") + "%'";

            Control uc_rep = LoadControl(ctrl);

            Repeater rptJobsList = (Repeater)uc_rep.FindControl("rptJobsList");

            DBUtility.Pagination pagination = new DBUtility.Pagination();
            try
            {
                if (!string.IsNullOrEmpty(RequestBase.GetString("index")))
                    pagination.Sequence = Convert.ToInt32(RequestBase.GetString("index"));
                else
                    pagination.Sequence = 1;

                pagination.ItemUnit = "条记录";
                pagination.SortSentence = " ORDER BY LineNum DESC";
                pagination.Primarykey = "JobId";
                pagination.SearchField = "*";
                pagination.TableName = "vw_JobsBase";
                if (!string.IsNullOrEmpty(RequestBase.GetString("pagesize")))
                    pagination.PageSize = Convert.ToInt32(RequestBase.GetString("pagesize"));
                else
                    pagination.PageSize = 20;
                pagination.UrlStr = "";
                pagination.ImagesPath = "../_imgs/grid/";
                pagination.SearchSentence = SQL.Trim();

                rptJobsList.DataSource = pagination.GetDataTable();
                rptJobsList.DataBind();

                pagination.PageBarFlexigrid();
                uc_rep.GetType().GetProperty("PaginationBar").SetValue(uc_rep, pagination.PageBar, null);

                StringBuilder strb = new StringBuilder();
                StringWriter sw = new StringWriter(strb);
                HtmlTextWriter htw = new HtmlTextWriter(sw);

                rptJobsList.RenderControl(htw);

                BLL.AjaxHelper.ResponseWrite(strb.ToString());
            }
            catch (System.Threading.ThreadAbortException err)
            {
                //nothing   to   do   
            }
            catch (Exception err)
            {
                BLL.AjaxHelper.ResponseWrite(err.Message);
            }
        }
        #endregion

        #region 删除
        /// <summary>
        /// 删除目录
        /// </summary>
        void Delete()
        {
            string itemsid = RequestBase.GetFormString("itemsid").Replace("'", "").Replace("'", "");

            string res = "";
            foreach (string objectid in itemsid.Split(','))
            {  
                /// 逻辑删除商品记录
                BLL.HR.JobsBase.Delete(objectid);

                res = "succeeded";
            }

            BLL.AjaxHelper.ResponseWrite(res);
        } 
        #endregion
    }
}
